
package com.wa.nashorn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {
    private static final Logger LOGGER = LoggerFactory.getLogger(SpringUtils.class);
    private static SpringUtils s_instance;
    private ApplicationContext applicationContext;

    public SpringUtils() {
        s_instance = this;
    }

    public static ApplicationContext getApplicationContext() {
        return s_instance.applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static <T> T getComponent(Class<T> clz) {
        try {
            return s_instance.applicationContext.getBean(clz);
        } catch (NoSuchBeanDefinitionException var2) {
            LOGGER.warn(var2.getMessage());
        } catch (Exception var3) {
            LOGGER.warn("Unexpected exception", var3);
        }

        return null;
    }
}
